<?php
/**
+-----------------------------------------------------------------------+
* @autor 张涛 <tonera at gmail.com>;
* @since 2006-6-21
* @version $Id: dkptable.php,v 1.3.2 tonera$
* @description	dkptable数据下载器.仅适用于1.3.2以后版本2006-7-20
+-----------------------------------------------------------------------+
*/
error_reporting(0);
header("Content-Type: text/html; charset=UTF-8");
if($_GET['do']!='download') {
	echo("WOW-DKPer1.3.2's plugin for DKPTable <br />");
	echo "请访问<a href=".$_SERVER['PHP_SELF']."?do=download>".$_SERVER['PHP_SELF']."?do=download</a>下载dkp.lua文件到您的插件目录下.";
	exit;
}

if(file_exists("./php/config.inc.php")) {
	$cfile	= "./php/config.inc.php";
}elseif(file_exists("../php/config.inc.php")) {
	$cfile	= "../php/config.inc.php";
}else {
	echo "请先安装wowdkper 1.3或以上版本.";
}
require_once $cfile;
require_once CHINO_MODPATH.'/config/config.inc.php';
require_once CHINO_LIBPATH.'/adodb.inc.php';


$object = ADONewConnection(DBTYPE);
$object->connect(DBHOST, DBUSER, DBPASS, DBNAME);
$mysqlvrs		= $object->execute("select version()");
$mysqlversion	= $mysqlvrs->fields[0];
if($mysqlversion > '4.1') {
	$object->execute("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
	if($mysqlversion > '5.0.1') {
		$object->execute("SET sql_mode=''");
	}
}


//$object->debug	= true;

//query copy
$copyarray		= array();
$userdata		= array();
$membercount	= array();
$sql	= "select * from ".TABLEHEAD."_copy";
$rs		= $object->execute($sql);
$i		= 0;
while(!$rs->EOF) {
	$copyarray[]	= $rs->fields;
	$cid			= $rs->fields['id'];
	//query spent dkp
	$sql			= "select uid,sum(ABS(value)) as value from ".TABLEHEAD."_itemdis where (value < 0 or stat='-1') and cid='$cid' group by uid";
	$srs			= $object->execute($sql);
	while(!$srs->EOF) {
		$spentdkp[$srs->fields['uid']]	= $srs->fields['value'];
		$srs->MoveNext();
	}
	//query member's dkp per copy	
	$sql			= "select a.uid as uid,a.dkpvalue as value,b.name as member,c.name as class from ".TABLEHEAD."_dkpvalues as a left join ".TABLEHEAD."_user as b on a.uid=b.id left join ".TABLEHEAD."_work as c on b.workid=c.id where a.copyid='$cid' and b.name is not null ";
	$urs			= $object->execute($sql);
	$j				= 0;
	$userdkp		= array();
	while(!$urs->EOF) {
		$urs->fields['spent']	= $spentdkp[$urs->fields['uid']]?$spentdkp[$urs->fields['uid']]:0;
		$urs->fields['class']	= $urs->fields['class']?$urs->fields['class']:'unknown';
		$userdkp[]	= $urs->fields;
		$j++;
		$urs->MoveNext();
	}
	$membercount[$i]	= $j;
	$userdata[$i]		= $userdkp;
	$i++;
	$rs->MoveNext();
}
//var_dump($i);
$object->close();

$outstr	= "-- DKP Table generated by WOW-DKPer ".date("Y-m-d H:i:s")."\n\n";
$outstr	.= "DKP_tables = {}\n";

for($k=0; $k<$i; $k++) {
	$cnum	= $k+1;
	$outstr	.= "DKP_tables[$cnum] = {\n";
	$unum	= count($userdata[$k]);
	for($y=0; $y<$unum; $y++) {
		switch($userdata[$k][$y]['class']) {
			case '盗贼':
			case 'rogue':
			case '盜賊':
				$class	= 'rogue';
			break;
			case '战士':
			case 'warrior':
			case '戰士':
				$class	= 'warrior';
				break;
			case '法师':
			case 'mage':
			case '法師':
				$class	= 'mage';
				break;
			case '牧师':
			case 'priest':
			case '牧師':
				$class	= 'priest';
				break;	
			case '德鲁伊':
			case 'druid':
			case '德魯伊':
				$class	= 'druid';
				break;
			case '猎人':
			case 'hunter':
			case '獵人':
				$class	= 'hunter';
				break;
			case '术士':
			case 'warlock':
			case '術士':
				$class	= 'warlock';
				break;
			case '萨满':
			case '萨满祭司':
			case 'shaman':
			case '薩滿':
			case '薩滿祭司':
				$class	= 'shaman';
				break;	
			case '圣骑士':
			case 'paladin':
			case '聖騎士':
				$class	= 'paladin';
				break;
			case '死亡骑士':
			case 'dead knight':
			case '死亡騎士':
				$class	= 'dead knight';
				break;
			default:
				$class	= 'unknown';
		}
		$outstr	.= '{ nick="'.$userdata[$k][$y]['member'].'", rank="", class=DKPT_'.$class.', current='.$userdata[$k][$y]['value'].', spent='.$userdata[$k][$y]['spent'].' }';
		if($y==($unum-1)) {
			$outstr	.= "\n";
		}else {
			$outstr	.= ",\n";
		}
	}
	$outstr	.= "};\n";
	$outstr	.= "DKP_tables[".$cnum."].title = \"".$copyarray[$k]['name']."\";\n";
	$outstr	.= "DKP_tables[".$cnum."].url = \"".$_SERVER['PHP_SELF']."\";\n";
	$outstr	.= "DKP_tables[".$cnum."].members = ".$membercount[$k].";\n";

}

$outstr	.= "DKP_tables_count = ".$i."\n";
$outstr	.= "DKP_tables_timestamp = \"".date("Y-m-d H:i:s")."\"\n";


header('Cache-control: private');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".strlen($outstr));
Header("Content-Disposition: attachment; filename=dkp.lua");
echo $outstr;
exit;

?>